// pages/measure/measure.js
var util = require('../../utils/util')
var app = getApp()
Page({
  data: {
    imgUlr: [
      // { src: 'https://static.magicmanufactory.com/appointment2/static/media/appoint_02.f41b1f42.jpg', title: '', id: '1' },
      { src: 'http://static.magicmanufactory.com/appointmentCommon/static/media/appoint_03.a3fb0803.jpg', title: '', id: '2' },
      // { src: 'https://static.magicmanufactory.com/appointment2/static/media/appoint_04.a28ccf95.jpg', title: '', id: '3' },
      { src: 'http://static.magicmanufactory.com/appointmentCommon/static/media/appoint_04.9f285430.jpg', title: '', id: '3' },
      { src: 'http://static.magicmanufactory.com/appointmentCommon/static/media/appoint_05.6a0fd2ed.jpg', title: '', id: '4' },
      { src: 'http://static.magicmanufactory.com/appointment2/static/media/appoint_06.30e20b7c.jpg', title: '', id: '5' },
      { src: 'http://static.magicmanufactory.com/appointmentCommon/static/media/appoint_07.72f18c0b.jpg', title: '', id: '6' },
    ],
    //location: "",微信定位获取到的位置信息
    scrollTop: 0,
    height: app.globalData.deviceInfo.windowHeight,
    width: `width: ${app.globalData.deviceInfo.windowWidth}px;`,
    detailedaddress:"",//详细地址
    name:"",//姓名
    phone:"",//电话
    remark:"",//备注


    //picker组件数据
    multiArray: [['青岛市', '杭州市', '广州市', '北京市'], ['市南区', '市北区', '崂山区', '李沧区', '城阳区', '即墨区']],//
    objectMultiArray: [
      [
        {
          id: 0,
          name: '青岛市'
        },
        {
          id: 1,
          name: '杭州市'
        },
        {
          id: 2,
          name: '广州市'
        },
        {
          id: 3,
          name: '北京市'
        }
      ], [
        {
          id: 0,
          name: '市南区'
        },
        {
          id: 1,
          name: '市北区'
        },
        {
          id: 2,
          name: '崂山区'
        },
        {
          id: 3,
          name: '李沧区'
        },
        {
          id: 3,
          name: '城阳区'
        },
        {
          id: 4,
          name: '即墨区'
        }
      ]
    ],
    multiIndex: [0, 0], //


  },
  //picker事件处理
  //
  bindMultiPickerChange: function (e) {
   
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      multiIndex: e.detail.value
    })
  },
  //
  bindMultiPickerColumnChange: function (e) {
    console.log('********修改的列为', e.detail.column, '，********值为', this.data.multiArray[0][e.detail.value]);
    var data = {
      //array为显示的数据
      multiArray: this.data.multiArray,
      //index为下标数据
      multiIndex: this.data.multiIndex
    };
    data.multiIndex[e.detail.column] = e.detail.value;
    switch (e.detail.column) {
      //点击第一列
      case 0:
      //判断选择的第一列值
        switch (data.multiIndex[0]) {
          case 0:
            data.multiArray[1] = ['市南区', '市北区', '崂山区', '李沧区', '城阳区', '即墨区'];
            break;
          case 1:
            data.multiArray[1] = ['上城区', '下城区', '江干区', '拱墅区', '西湖区', '滨江区', '萧山区', '余杭区', '富阳区', '临安区', '建德县', '桐庐县', '淳安县'];
            break;
          case 2:
            data.multiArray[1] = ['越秀区', '海珠区', '荔湾区', '天河区', '白云区', '黄埔区', '花都区', '番禺区', '萝岗区', '南沙区', '从化市', '增城市'];
            break;
          case 3:
            data.multiArray[1] = [''];
            break;
        }
        // data.multiIndex[1] = 0;
        // data.multiIndex[2] = 0;
        break;
        //点击第二列
      case 1:
        // console.log("*******点击了第二列*****值为", this.data.multiArray[1][e.detail.value]);
        break;
    }
    this.setData(data);
  },
  onLoad: function (options) {
    console.log(options)
    console.log('123')
  },
  onReady: function () {
  },
  onShow: function () {
  },
  onHide: function () {
  },
  onUnload: function () {
  },
  sendMeasure: function (e) { //发送测量数据
    let errorStr = ''
    if (!this.data.name) {
      errorStr = '请输入姓名'
    } else if (!this.data.phone) {
      errorStr = '请输入电话号码'
    } else if (!this.data.detailedaddress) {
      errorStr = '请输入详细地址'
    }
    console.log("name", this.data.name);
    console.log("phone", this.data.phone);
    console.log("detailed", this.data.detailedaddress);
    console.log("错误信息",errorStr);
    if (errorStr.length > 0) {
      wx.showModal({
        title: '格式有误',
        content: errorStr,
        success: function (res) {
        }
      })
      return
    }

    let date = new Date()
    let dataStr = util.dateFormat(date)
    let measuredata = {
      "addressDetailed": this.data.detailedaddress,
      "orderName": this.data.name,
      "phoneNo": this.data.phone,
      "appointTime": dataStr,
      "cityXd": this.data.multiArray[0][this.data.multiIndex[0]] + this.data.multiArray[1][this.data.multiIndex[1]],
      "countyXd": "中国",
      "provinceXd": "a",
      "orderNote": this.data.remark,
      "adsense": "a",
      "channel": "xiaochengxu"
    }
    // let data = {
    //   "orderName": this.name,     //姓名
    //   "phoneNo": this.phone,      //手机号
    //   "cityXd": this.multiArray[0][multiIndex[0]] + this.multiArray[1][multiIndex[1]],  //城市
    //   "addressDetailed": this.detailed,  //量体人详细地址
    //   "appointTime": dataStr,     //预约量体时间
    //   "channel": "WeChat",         //下单渠道
    //   "countyXd": district,       //量体人区县
    //   "address_detailed": "TODO"//详细地址
    // }
    // orderName: this.refs.userName.value,
    // phoneNo:tempPhoneNo,
    // cityXd:city,
    // addressDetailed:this.refs.locationDetail.value,
    // appointTime:tempDate,
    //     orderNote:tempNote,//婚礼纪专属备注字段
    //     provinceXd:province, 量体人省份
    //     adsense: adsenseValue,//广告位名称
    // channel:channelValue
    wx.showToast({ title: 'loading..', icon: 'loading', uration: 10000 })
    console.log("传入的参数为", measuredata)
    // debugger
    wx.request({
      url: "https://www.magicmanufactory.com/c2mwebservice/api/v2/public/measure/newOrder/nonMembers",
      method: 'POST',
      header: {
        'Accept': 'application/json',
        // 'C2M-Identify': 'Platform:iOS,APP Version:6.300,OS Version:10.2,Device Type:xiaochengxu',
        'C2M-Identify': 'Platform:Android,APP Version:6.300,OS Version:10.2,Device Type:xiaochengxu'

      },
      data: measuredata,
      success: function (res) {
        wx.showModal({
          title: '预约反馈信息',
          content: res.data.returnMsg,
          success: function (res) {
          }
        })
      },
      fail: function (res) {
        wx.showModal({
          title: '网络错误',
          content: "请求超时",
          success: function (res) {
          }
        })
      }
    })
    // util.postRequest('c2mwebservice/api/v2/public/measure/newOrder/nonMembers', measuredata, function (res) {
    //   console.log(res);
    //   wx.hideToast()
    //   wx.showModal({
    //     title: '',
    //     content: '预约成功',
    //     showCancel: false,
    //     success: function (res) {
    //       wx.switchTab({
    //         url: '../index/index'
    //       })
    //     }
    //   })

    // }, function (res) {
    //   console.log(res);
    //   // wx.showToast({
    //   //   title: res.data.returnMsg,
    //   //   duration: 2000
    //   // })
    // })
  },

  inputEnd: function (e) {//输入框失去焦点时获取值
    let id = e.currentTarget.id;
    if (id == "name") {
      this.setData({
        name: e.detail.value
      })
    } if (id == "phone") {
      this.setData({
        phone: e.detail.value
      })
    } if (id == "remark") {
      this.setData({
        remark: e.detail.value
      })
    } if (id = "detailedaddress") {
      this.setData({
        detailedaddress: e.detail.value
      })
    }

  },
  // chooseAddress: function (e) { //选择地址
  //     let that = this
  //     wx.chooseLocation({//打开地图选择位置
  //         success: function (res) {
  //             that.setData({ location: res.address })
  //         }
  //     })
  // },
  toPhoneTel: function (e) {//单击拨打电话
   let id=e.currentTarget.id;
   if(id =="kefu"){
     wx.makePhoneCall({
       phoneNumber: '4009-899-899'
     })
   }if(id=="qijiandian"){
     wx.makePhoneCall({
       phoneNumber: '0532-88599616'
     })
   }
  },
  goTop: function (e) { //滚动到页面顶部
    this.setData({
      scrollTop: 0
    });
  }
})